package com.zhongshengnetwork.rightbe.dbservice;

import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.dbmodel.GroupUserdbModel;
import com.zhongshengnetwork.rightbe.dbmodel.LogindbModel;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * Created by zhongsheng-luojianlan on 16/11/9.
 */

public class GroupUserdbService {
    //插入或者更新数据
    public static boolean saveOrUpdate(GroupUserdbModel model){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.saveOrUpdate(model);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @return
     */
    //查找数据
    public static List<GroupUserdbModel> getdbModel(String groupid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(GroupUserdbModel.class).where("groupid","==",groupid) != null)
               return db.selector(GroupUserdbModel.class).where("groupid","==",groupid).findAll();
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    //查找数据
    public static GroupUserdbModel getdbUserModel(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            return db.findById(GroupUserdbModel.class,id);
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
    }

    //查找团长
    public static GroupUserdbModel getdbOwnerUserModel(String groupid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            List<GroupUserdbModel> list = null;
            if (db.selector(GroupUserdbModel.class).where("groupid","==",groupid).and("type","==",2) != null)
              list = db.selector(GroupUserdbModel.class).where("groupid","==",groupid).and("type","==",2).findAll();
            if (list != null && list.size() > 0){
                return list.get(0);
            }
            return null;
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
    }

    //查找昵称
    public static String getUserGroupName(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.findById(GroupUserdbModel.class,id) != null){
                String name = db.findById(GroupUserdbModel.class,id).getName();
                name = CommonUtils.formatString(name);
                return name.length()==0 ? db.findById(GroupUserdbModel.class,id).getNickname():name;
            }
            return "";
        } catch (DbException e) {
            e.printStackTrace();
            return "";
        }
    }

    //判断是否是群成员
    public static boolean isGroupMember(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.findById(GroupUserdbModel.class,id) == null){
                return false;
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //统计数量
    public static long getdbCount(String groupid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(GroupUserdbModel.class).where("groupid","==",groupid) != null)
              return db.selector(GroupUserdbModel.class).where("groupid","==",groupid).count();
        } catch (DbException e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }

    //删除数据
    public static boolean deleteModel(){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.delete(GroupUserdbModel.class);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除数据
    public static boolean deleteGroupUser(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.deleteById(GroupUserdbModel.class,id);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //更新用户头像和昵称信息
    public static boolean updateGroupUserInfo(String userid, String name, String header){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            List<GroupUserdbModel> list = db.selector(GroupUserdbModel.class).where("userid","==",userid).findAll();
            if (list != null && list.size() > 0){
               for (GroupUserdbModel groupUserdbModel : list){
                   groupUserdbModel.setNickname(name);
                   groupUserdbModel.setHeader(header);
                   db.saveOrUpdate(groupUserdbModel);
               }
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }
}
